package com.cxhc.gank;


import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 网络请求工具类
 * 基于okhttp
 */
public class NetUtils {

    public static final String TAG = NetUtils.class.getSimpleName();

    Retrofit retrofit;
    String url;

    OkHttpClient okHttpClient;

    public  NetUtils() {
        //初始化参数操作

        okHttpClient = new OkHttpClient.Builder()
                .callTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .connectTimeout(500, TimeUnit.MILLISECONDS)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.GANK_URL)//基础URL 建议以 / 结尾
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();


    }


    public void getFuliPics(final IFuLiCallback callback) {
        DataService dataService = retrofit.create(DataService.class);
        Observable<FuliBean> data = dataService.getFuliPicService();
        data.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FuliBean>() {
                    @Override
                    public void onCompleted() {
                        Log.e(TAG, "onCompleted: ");
                        callback.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: " + e.getMessage());
                        callback.onError(e);
                    }

                    @Override
                    public void onNext(FuliBean fuliBean) {
                        Log.e(TAG, "onNext: " + fuliBean.toString());
                        callback.onNext(fuliBean);
                    }
                });


    }
//    public <T> T get() {
//        return T;
//    }
//
//    public <T> T post(Object object) {
//        return T;
//    }
}
